package com.inspur.s57.datacatalog.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspur.s57.datacatalog.entity.TreeModel;
import com.inspur.s57.datacatalog.service.impl.TreeModelServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@RestController
@RequestMapping("api/model")
public class ModelController {

    @Autowired
    TreeModelServiceImpl treeModelService;

    @GetMapping("/getOne")
    public JSONObject getTreeModel(@RequestParam(value = "id") Integer id){
        TreeModel model=treeModelService.getTreeModel(id);
       return JSONObject.parseObject(model.getTreeModel());
    }

    @GetMapping("/list")
    public List<TreeModel> listModel(){
        return  treeModelService.listTreeModel();
    }

    @PostMapping("/save")
    public TreeModel saveTreeModel(@RequestParam(value = "name") String name,@RequestBody JSONObject model){

        TreeModel treeModel=new TreeModel();
        treeModel.setName(name);
       treeModel.setTreeModel(model.toJSONString());
        return treeModelService.saveTreeModel(treeModel);
    }
}
